package hxy.scan.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hxy.scan.server.dao.mapper.LocationMapper;
import hxy.scan.server.dao.model.LocationModel;
import hxy.scan.server.entity.BaseResponse;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LocationService extends ServiceImpl<LocationMapper, LocationModel> {

    public BaseResponse saveLocation(LocationModel locationModel) {
        boolean save = save(locationModel);
        if (save) {
            return BaseResponse.success();
        } else {
            return BaseResponse.error("位置存储到数据库失败");
        }
    }

    public BaseResponse getByUserId(Long userId, long current) {
        QueryWrapper<LocationModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("id");
        IPage<LocationModel> page = new Page<>(current, 10);
        page = page(page, queryWrapper);
        return BaseResponse.success(page);
    }
}
